import axios from 'axios'
import type { AxiosInstance } from 'axios'
import type { ZFRequestInterceptors, ZFRequestConfig } from './type'
import { ElLoading } from 'element-plus'
// import { LoadingInstance } from 'element-plus/lib/components/loading/src/types'
import { LoadingInstance } from 'element-plus/es/components/loading/src/loading'

class ZFRequest {
  instance: AxiosInstance
  interceptors?: ZFRequestInterceptors
  showLoading: boolean
  loading?: LoadingInstance
  constructor(config: ZFRequestConfig) {
    this.instance = axios.create(config)
    this.showLoading = false
    this.interceptors = config.interceptors
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptor,
      this.interceptors?.requestInterceptorCatch
    )
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptor,
      this.interceptors?.responseInterceptorCatch
    )

    //创建所有实例都有的拦截器
    this.instance.interceptors.request.use(
      (config) => {
        console.log('所有实例都有的拦截器:请求成功')
        if (this.showLoading) {
          this.loading = ElLoading.service({
            lock: true,
            text: '正在请求数据..',
            background: 'rgba(0, 0, 0, 0.5)'
          })
        }

        return config
      },
      (err) => {
        console.log('所有实例都有的拦截器:请求失败')
        return err
      }
    )
    this.instance.interceptors.response.use(
      (res) => {
        setTimeout(() => {
          this.loading?.close()
        }, 500)
        console.log('所有实例都有的拦截器:响应成功')
        const data = res.data
        if (data.returnCode === '-1001') {
          console.log('请求失败，错误信息')
        } else {
          return data
        }
        return config
      },
      (err) => {
        setTimeout(() => {
          this.loading?.close()
        }, 500)

        if (err.response.status === 404) {
          console.log('404的错误')
        }
        console.log('所有实例都有的拦截器:响应失败')
        return err
      }
    )
  }
  request<T>(config: ZFRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      if (config.interceptors?.requestInterceptor) {
        config = config.interceptors.requestInterceptor(config)
      }
      this.showLoading = config.showLoading ?? false
      this.instance
        .request<any, T>(config)
        .then((res) => {
          if (config.interceptors?.responseInterceptor) {
            res = config.interceptors.responseInterceptor(res)
          }
          resolve(res)
        })
        .catch((err) => {
          reject(err)
        })
    })
  }
  get<T>(config: ZFRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'GET' })
  }
  post<T>(config: ZFRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'POST' })
  }
  delete<T>(config: ZFRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'DELETE' })
  }
  patch<T>(config: ZFRequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: 'PATCH' })
  }
}

export default ZFRequest
